package com.fjec.devops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.DateUtil;
import com.fjec.devops.entity.StatAllUserNum;
import com.fjec.devops.entity.StatOrderInfo;
import com.fjec.devops.mapper.StatAllUserNumMapper;
import com.fjec.devops.service.IStatAllUserNumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.devops.view.AllUserNumVO;
import com.sun.javafx.collections.MappingChange;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * <p>
 * 014.统计-用户数量表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-09-18
 */
@Service
public class StatAllUserNumServiceImpl extends ServiceImpl<StatAllUserNumMapper, StatAllUserNum> implements IStatAllUserNumService {

    @Resource
    private StatAllUserNumMapper mapper;

    @Override
    public JsonResult statAllUserNum(Integer days) throws Exception {
        // 通过days计算开始日期和结束日期
        LocalDate endTime = DateUtil.nowLd();
        LocalDate startTime = DateUtil.ldMinus(endTime, ChronoUnit.DAYS, days);
        List<StatAllUserNum> entityList = mapper.statAllUserData(startTime, endTime);
        List<String> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();
        for (StatAllUserNum entity : entityList) {
            xList.add(DateUtil.format(entity.getStatDate(), "MM月dd日"));
            yList.add(entity.getUserNum());
        }
        AllUserNumVO view = new AllUserNumVO();
        view.setXList(xList);
        view.setYList(yList);
        return JsonResult.success()
                .data(view)
                .build();
    }

    @Override
    public JsonResult statUserNumInfo(long id) throws Exception {
        return null;
    }

    @Override
    public JsonResult statUserNumAdd(int userNum) throws Exception {
        // 统计结果记录的日期是出结果的当天
        StatAllUserNum entity = new StatAllUserNum();
        LocalDateTime now = LocalDateTime.now();
        entity.setStatDate(now.toLocalDate());
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        entity.setUserNum(userNum);
        mapper.insert(entity);
        return JsonResult.success().build();
    }

    @Override
    public JsonResult statUserNumModify(long id, int userNum) throws Exception {
        // 统计结果记录的日期是出结果的当天
        QueryWrapper<StatAllUserNum> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        StatAllUserNum entity = new StatAllUserNum();
        LocalDateTime now = LocalDateTime.now();
        entity.setStatDate(now.toLocalDate());
        entity.setUpdateTime(now);
        entity.setUserNum(userNum);
        mapper.update(entity, wrapper);
        return JsonResult.success().build();
    }

    @Override
    public JsonResult statUserNumDel(long id) throws Exception {
        QueryWrapper<StatAllUserNum> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        mapper.delete(wrapper);
        return JsonResult.success().build();
    }

    @Override
    public JsonResult statUserNumFakeData() throws Exception {
        LocalDateTime now = LocalDateTime.now();
        int before = 365;
        double rate = 0;
        int counter = 0;
        for(int i =0; i < before; i++){
            StatAllUserNum entity = new StatAllUserNum();
            LocalDateTime dt = now.minusDays(before - i);
            rate = Math.random();
            if(dt.getDayOfWeek().equals("FRIDAY")){
                rate = rate * 1.3;
            }
            if(dt.getDayOfWeek().equals("SATURDAY")){
                rate = rate * 2.3;
            }
            if(dt.getDayOfWeek().equals("SUNDAY")){
                rate = rate * 3.3;
            }
            counter = counter + (int)Math.floor(3 * rate * 1024 * i);
            entity.setUserNum(counter);
            entity.setStatDate(dt.toLocalDate());
            entity.setUpdateTime(dt);
            entity.setCreateTime(dt);
            // 这样会不会数据库压力太大
            mapper.insert(entity);
        }
        return JsonResult.success().build();
    }

}
